package redis

import (
	"context"
	"github.com/go-redis/redis/v8"
	"time"
)

var ctx = context.Background()

type RedisClient struct {
	client *redis.Client
}

func NewRedisClient(addr string) *RedisClient {
	client := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	return &RedisClient{client: client}
}

func (r *RedisClient) SetCode(phone, code string) error {
	return r.client.Set(ctx, phone, code, 5*time.Minute).Err()
}

func (r *RedisClient) GetCode(phone string) (string, error) {
	return r.client.Get(ctx, phone).Result()
}
